package so.ifl.manager.staff;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.web.DoradoContext;

import so.ifl.commons.tools.Constant;
import so.ifl.commons.tools.DateUtil;
import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.commons.tools.StringHelper;
import so.ifl.commons.tools.except.SessionTimeOutException;
import so.ifl.servcie.TransactionService;
import so.ifl.vo.AccountVo;
import so.ifl.vo.TranRecordVo;
import so.ifl.vo.Union1Vo;

/**
 *create by aaron.lu
 *create at 2012-2-4 下午3:38:07
 *<mailto: atyuan.lu@gmail.com>
 **/
@Component("studentTransactionManager")
public class StudentTransactionManager {
	
	@Autowired
	private TransactionService transactionService;
	
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 保存交易记录
	 * create by aaron.lu
	 * create at 2012-2-5 上午2:13:13
	 * <mailto: atyuan.lu@gmail.com>
	 * @param tranRecordVos
	 * @throws Exception
	 *
	 */
	@DataResolver
	public void saveTranRecord(Collection<TranRecordVo> tranRecordVos) throws Exception{
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		
		if (null == accountVo) {
			 throw new SessionTimeOutException("登录超时。。");
		}
		
		for (TranRecordVo tranRecordVo : tranRecordVos) {
			
			EntityState entityState = EntityUtils.getState(tranRecordVo);
			if (EntityState.NEW.equals(entityState)) {
				//如果新增住状态，则新增收费记录
				tranRecordVo.setStudentCreateTime(null != tranRecordVo.getTranNode()?new Date(new Long(tranRecordVo.getTranNode())):null);
				tranRecordVo.setTranNode(Constant.NEW_TRAN_RECORD);
				tranRecordVo.setAccoutId(accountVo.getAccoutId());
				tranRecordVo.setCreateUser(accountVo.getAccountLoginName());
				tranRecordVo.setCreateTime(new Date());
				tranRecordVo.setCreateAccountOwner(accountVo.getAccountOwner());
				tranRecordVo.setCompanyId(accountVo.getCompanyId());
				
				transactionService.tran(tranRecordVo);
			}else if (EntityState.MODIFIED.equals(entityState)) {
				//如果更新记录，则是冲销记录				
				TranRecordVo tranRecordVo_ = new TranRecordVo();
				BeanUtils.copyProperties(tranRecordVo, tranRecordVo_);
				tranRecordVo_.setTranId(null);
				tranRecordVo_.setCreateUser(accountVo.getAccountLoginName());
				tranRecordVo_.setCreateTime(new Date());
				tranRecordVo_.setIsChargeAgainst("1");
				tranRecordVo_.setMoney(null != tranRecordVo_.getMoney()?(0 - tranRecordVo_.getMoney()):0);
				tranRecordVo_.setTranCode(tranRecordVo_.getTranCode() + "C");
				tranRecordVo_.setTranNode(Constant.CANCEL_TRAN_RECORD);
				
				transactionService.tran(tranRecordVo_);
				
				tranRecordVo.setTypeOfTran("4");//无效记录，已经冲销，类型为4
				tranRecordVo.setTranNode(Constant.MODIFY_TRAN_RECORD);
				tranRecordVo.setLastModifyUser(accountVo.getAccountLoginName());
				tranRecordVo.setLastModifyTime(new Date());
				transactionService.updateTranRecord(tranRecordVo);
			}
		}
	}
	
	/**
	 * 
	 * create by aaron.lu
	 * create at 2012-3-25 下午10:54:06
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getTranInfo(Page<TranRecordVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (params == null) {
			params = new HashMap<String, Object>();
		}else{
			Date less_time = (Date)params.get("less_time_");
			Date bigger_time = (Date)params.get("bigger_time");
//			Date bigger_time = less_time;//只去当天的 <20130220>注释，增加时间段的查询
			
			if (null != less_time && null != bigger_time) {
				try {
					params.put("less_time_", DateUtil.getFirstSecondInDay(less_time));
					params.put("bigger_time", DateUtil.getLastSecondInDay(bigger_time));
				} catch (Exception e) {
					logger_.error("提取查询参数失败：" + e.getMessage());
				}
				
			}
		}

		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			params.put("company_id", accountVo.getCompanyId());
			
			List<TranRecordVo> list = transactionService.queryTranInfo(params, offset, limmit);
			int total = transactionService.countTranInfo(params);
			page.setEntities(list);
			page.setEntityCount(total);
		}else{
			logger_.error("登录超时。。");
			
			page.setEntities(new ArrayList<TranRecordVo>());
			page.setEntityCount(0);
		}
		
		
	}
	
	/**
	 * 求和交易信息
	 * create by aaron.lu
	 * create at 2012-3-25 下午11:01:23
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	@Expose
	public Map<String, Object> sumTranInfo(Map<String, Object> params){
		Map<String, Object> re = new HashMap<String, Object>();
		if (params == null) {
			params = new HashMap<String, Object>();
		}else{
			Date less_time = (Date)params.get("less_time_");
			Date bigger_time = (Date)params.get("bigger_time");
//			Date bigger_time = less_time;//只去当天的<20130220>注释，增加时间段的查询
			
			if (null != less_time && null != bigger_time) {
				try {
					params.put("less_time_", DateUtil.getFirstSecondInDay(less_time));
					params.put("bigger_time", DateUtil.getLastSecondInDay(bigger_time));
				} catch (Exception e) {
					logger_.error("提取查询参数失败：" + e.getMessage());
				}
				
			}
		}
		
		
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			params.put("company_id", accountVo.getCompanyId());
			
			float sum = transactionService.sumTranInfo(params);
			
			re.put("sum", sum);
		}else{
			logger_.error("登录超时。。");
		}
		
		return re;
	}

}
